package java44th.group.service.wx.auth;

import java44th.group.bean.po.MarketUser;
import java44th.group.bean.po.MarketUserExample;
import java44th.group.config.aliyun.config.AliyunComponent;
import java44th.group.mapper.MarketUserMapper;
import java44th.group.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/14 9:38
 * @Description:
 * @Version 1.0
 */
@Transactional
@Service
public class WxAuthServiceImpl implements WxAuthService {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    AliyunComponent aliyunComponent;


    @Override
    public void reset(Map map) {
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andMobileEqualTo((String) map.get("mobile"));
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if (marketUsers.get(0) != null) {
            marketUsers.get(0).setPassword((String) map.get("password"));
            marketUsers.get(0).setUpdateTime(new Date());
            marketUserMapper.updateByPrimaryKeySelective(marketUsers.get(0));
        }

    }

    @Override
    public int register(Map map) {

        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andMobileEqualTo((String) map.get("username"));
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if (marketUsers.size() != 0) {

            //用户名重复
            return 1;
        }
        MarketUserExample marketUserExample1 = new MarketUserExample();
        marketUserExample1.createCriteria().andMobileEqualTo((String) map.get("mobile"));
        List<MarketUser> marketUsers1 = marketUserMapper.selectByExample(marketUserExample1);
        if (marketUsers1.size() != 0) {
            //手机号已注册
            return 2;
        }

        MarketUser marketUser = new MarketUser();
        marketUser.setPassword(Md5Utils.getMd5((String) map.get("password")));
        marketUser.setUpdateTime(new Date());
        marketUser.setAddTime(new Date());
        marketUser.setUsername((String) map.get("username"));
        marketUser.setMobile((String) map.get("mobile"));
        marketUser.setNickname((String) map.get("username"));
        marketUser.setUpdateTime(new Date());
        marketUser.setAddTime(new Date());
        marketUser.setLastLoginTime(new Date());
        marketUser.setAvatar("http://xyf12345.oss-cn-hangzhou.aliyuncs.com/58584704feaf4434bfe46ccc8105ba8b.jpg");
        marketUserMapper.insertSelective(marketUser);
        //注册成功
        return 3;


    }

    @Override
    public String regCaptcha(Map map) {

        String ssm = SSM();
        aliyunComponent.sendMsg((String) map.get("mobile"),ssm);


        return ssm;
    }


    //设置验证码
    public String SSM() {
        int TemplateParam;
        double random;
        random = Math.random();
        if (random > 0.1) {
            random = random * 10000;
        } else {
            random = random * 100000;
        }
        TemplateParam = (int) Math.floor(random);
        String coadParam = String.valueOf(TemplateParam);
        return coadParam;
    }
}
